import { useState } from 'react';
import { isFunction } from 'lodash-es';

import { useMemoizedFn } from './useMemoizedFn';

export type SetState<S extends Record<string, any>> = <K extends keyof S>(
    state: Pick<S, K> | null | ((prevState: Readonly<S>) => Pick<S, K> | S | null)
) => void;

const useSetState = <S extends Record<string, any>>(initialState: S | (() => S)): [S, SetState<S>] => {
    const [state, setState] = useState<S>(initialState);

    const setMergeState = useMemoizedFn((patch) => {
        setState((prevState) => {
            const newState = isFunction(patch) ? patch(prevState) : patch;
            return newState ? { ...prevState, ...newState } : prevState;
        });
    });

    return [state, setMergeState];
};

export { useSetState };
